package org.sninwo.lnbook.book.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.sninwo.lnbook.base.hibernate.IdEntity;

@Entity
@Table(name = "book")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Book extends IdEntity {
	private String name; //书名
	private String name2; //又名
	private String source; //出处（包括卷数）
	private String preface; //序跋（包括凡例）
	private String chorography; //地方志
	private String author; //朝代、作者（包括著作方式）
	private String aboutAuthor; //作者简介
	private String abstract_; //内容提要
	private String academicFeatures; //学术特点
	private String holdings; //版本和馆藏
	private String comment; //按语
	private String rolls; //卷次
	private Long typeId; //类型id
	private Long originalId; //原文id

	@Column(nullable = false)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName2() {
		return name2;
	}
	public void setName2(String name2) {
		this.name2 = name2;
	}
	public String getSource() {
		return source;
	}
	public void setSource(String source) {
		this.source = source;
	}
	public String getPreface() {
		return preface;
	}
	public void setPreface(String preface) {
		this.preface = preface;
	}
	public String getChorography() {
		return chorography;
	}
	public void setChorography(String chorography) {
		this.chorography = chorography;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getAboutAuthor() {
		return aboutAuthor;
	}
	public void setAboutAuthor(String aboutAuthor) {
		this.aboutAuthor = aboutAuthor;
	}
	@Column(name="abstract")
	public String getAbstract_() {
		return abstract_;
	}
	public void setAbstract_(String abstract_) {
		this.abstract_ = abstract_;
	}
	public String getAcademicFeatures() {
		return academicFeatures;
	}
	public void setAcademicFeatures(String academicFeatures) {
		this.academicFeatures = academicFeatures;
	}
	public String getHoldings() {
		return holdings;
	}
	public void setHoldings(String holdings) {
		this.holdings = holdings;
	}
	public String getComment() {
		return comment;
	}
	public void setComment(String comment) {
		this.comment = comment;
	}
	@Column(nullable = false)
	public Long getTypeId() {
		return typeId;
	}
	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}
	public Long getOriginalId() {
		return originalId;
	}
	public void setOriginalId(Long originalId) {
		this.originalId = originalId;
	}
	public String getRolls() {
		return rolls;
	}
	public void setRolls(String rolls) {
		this.rolls = rolls;
	}
	
	/**
	 * 返回原文内容
	 */
	@Transient
	public String toOriginal(){
		StringBuilder content = new StringBuilder();
		appendOriginal(content, this.getName());
		appendOriginal(content, this.getName2());
		appendOriginal(content, this.getAuthor());
		appendOriginal(content, this.getSource());
		appendOriginal(content, this.getRolls());
		appendOriginal(content, this.getAboutAuthor());
		appendOriginal(content, this.getAbstract_());
		appendOriginal(content, this.getAcademicFeatures());
		appendOriginal(content, this.getPreface());
		appendOriginal(content, this.getChorography());
		appendOriginal(content, this.getHoldings());
		appendOriginal(content, this.getComment());
		return content.toString().trim();
	}
	

	/**
	 * 返回书籍的大文本字段的内容总和，包括作者简介、内容提交等等字段
	 */
	@Transient
	public String getTextContent(){
		StringBuilder content = new StringBuilder();
		appendOriginal(content, this.getAboutAuthor());
		appendOriginal(content, this.getAbstract_());
		appendOriginal(content, this.getAcademicFeatures());
		appendOriginal(content, this.getPreface());
		appendOriginal(content, this.getChorography());
		appendOriginal(content, this.getHoldings());
		appendOriginal(content, this.getComment());
		return content.toString().trim();
	}
	
	private void appendOriginal(StringBuilder content, String fieldData){
		if (fieldData != null && fieldData.trim().length() > 0){
			content.append(fieldData.trim()).append("\n");
		}
	}
}
